package com.zuihai.app.mvp.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.zuihai.app.R;
import com.zuihai.app.mvp.constant.HomeConstant;
import com.zuihai.app.mvp.model.entity.FindItem;
import com.zuihai.app.mvp.ui.holder.BaseViewHolderWraper;

import java.util.List;

/**
 * Created by Administrator on 2018/1/30 0004.
 * 用的是发现的实体,首页栏目的样式
 *
 */

public class FindHomeRecyclerAdapter extends BaseQuickAdapter<FindItem, BaseViewHolderWraper> {

    private Context context;
    private int width;

    public FindHomeRecyclerAdapter(List<FindItem> infos, int width, Context context) {
        super(infos);
        this.width = width;
        this.context=context;
        setMultiTypeDelegate(new MultiTypeDelegate<FindItem>() {
            @Override
            protected int getItemType(FindItem homeItem) {
                return homeItem.getItemType();
            }
        });

        getMultiTypeDelegate()
                .registerItemType(HomeConstant.ONE_TYPE, R.layout.layout_item_home_one)
                .registerItemType(HomeConstant.TWO_TYPE, R.layout.layout_item_home_two)
                .registerItemType(HomeConstant.THREE_TYPE, R.layout.layout_item_home_three)
                .registerItemType(HomeConstant.FOUR_TYPE, R.layout.layout_item_home_four);
    }

    @Override
    protected void convert(BaseViewHolderWraper helper, FindItem item) {
        switch (helper.getItemViewType()) {
            case HomeConstant.ONE_TYPE:
                convertOneItem(helper, item);
                break;
            case HomeConstant.TWO_TYPE:
                convertTwoItem(helper, item);
                break;
            case HomeConstant.THREE_TYPE:
                convertThreeItem(helper, item);
                break;
            case HomeConstant.FOUR_TYPE:
                convertFourItem(helper, item);
                break;
            default:
                convertTwoItem(helper, item);
                break;
        }
    }

    private void convertOneItem(BaseViewHolderWraper helper, FindItem item) {

        List<FindItem.Picture> pictures = item.picture;
        ImageView ivCoverOne = helper.getView(R.id.iv_cover_one);
        ImageView ivCoverTwo = helper.getView(R.id.iv_cover_two);
        ImageView ivCoverThree = helper.getView(R.id.iv_cover_three);
        ViewGroup.LayoutParams lp = ivCoverOne.getLayoutParams();
        lp.width=width/3;
        lp.height = (int) (width / 3 * 0.666);
        LogUtils.e(lp.height);
        ivCoverOne.setLayoutParams(lp);
        ivCoverTwo.setLayoutParams(lp);
        ivCoverThree.setLayoutParams(lp);
        helper.setText(R.id.tv_title, item.title);
        helper.loadImage32(R.id.iv_cover_one, pictures.get(0).pictureUrl);
        helper.loadImage32(R.id.iv_cover_two, pictures.get(1).pictureUrl);
        helper.loadImage32(R.id.iv_cover_three, pictures.get(2).pictureUrl);
        //设置来源
        if (item.auther.length() > 0) {
            helper.setText(R.id.tv_name, item.auther);
        } else {
            helper.setText(R.id.tv_name, "笑嗨");
        }
        LinearLayout llComment=helper.getView(R.id.ll_comment);
        LinearLayout llFavour=helper.getView(R.id.ll_favour);
        if ("0".equals(item.commentCount)){
            llComment.setVisibility(View.GONE);
        }else {
            llComment.setVisibility(View.VISIBLE);
        }
        if ("0".equals(item.favourCount)){
            llFavour.setVisibility(View.GONE);
        }else {
            llFavour.setVisibility(View.VISIBLE);
        }
        //设置评论数量
        helper.setText(R.id.tv_comment, String.format("%s", item.commentCount + ""));
        //设置点赞数量
         helper.setText(R.id.tv_favour, String.format("%s", item.favourCount + ""));
    }

    private void convertTwoItem(BaseViewHolderWraper helper, FindItem item) {
        ImageView ivCover = helper.getView(R.id.iv_cover);
        ViewGroup.LayoutParams lp = ivCover.getLayoutParams();
        lp.height = (int) (width / 3 * 0.6667);
        ivCover.setLayoutParams(lp);
        helper.loadImage32(R.id.iv_cover, item.picture.get(0).pictureUrl);
        helper.setText(R.id.tv_title, item.title);
        //设置来源
        if (item.auther.length() > 0) {
            helper.setText(R.id.tv_name, item.auther);
        } else {
            helper.setText(R.id.tv_name, "笑嗨");
        }
        LinearLayout llComment=helper.getView(R.id.ll_comment);
        LinearLayout llFavour=helper.getView(R.id.ll_favour);
        if ("0".equals(item.commentCount)){
            llComment.setVisibility(View.GONE);
        }else {
            llComment.setVisibility(View.VISIBLE);
        }
        if ("0".equals(item.favourCount)){
            llFavour.setVisibility(View.GONE);
        }else {
            llFavour.setVisibility(View.VISIBLE);
        }
        //设置评论数量
        helper.setText(R.id.tv_comment, String.format("%s", item.commentCount + ""));
        //设置点赞数量
        helper.setText(R.id.tv_favour, String.format("%s", item.favourCount+ ""));
    }

    private void convertFourItem(BaseViewHolderWraper helper, FindItem item) {

        ImageView ivCover = helper.getView(R.id.iv_cover);

        ViewGroup.LayoutParams lp = ivCover.getLayoutParams();
        lp.height = (int) (width * 0.5625);
        ivCover.setLayoutParams(lp);
        helper.loadImage169(R.id.iv_cover, item.picture.get(0).pictureUrl);
    }

    private void convertThreeItem(BaseViewHolderWraper helper, FindItem item) {
        ImageView ivCover = helper.getView(R.id.iv_cover);
        ViewGroup.LayoutParams lp = ivCover.getLayoutParams();

        lp.height = (int) ((width) * 0.5625);
        ivCover.setLayoutParams(lp);
       /* ivCover.post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams layoutParams = ivCover.getLayoutParams();
                layoutParams.height = (int) (ivCover.getMeasuredWidth() * 0.5625);
                ivCover.setLayoutParams(layoutParams);
            }
        });*/
        helper.loadImage169(R.id.iv_cover, item.picture.get(0).pictureUrl);

        helper.setText(R.id.tv_title, item.title);
        if (item.auther.length() > 0) {
            helper.setText(R.id.tv_name, item.auther);
        } else {
            helper.setText(R.id.tv_name, "笑嗨");
        }
        LinearLayout llComment=helper.getView(R.id.ll_comment);
        LinearLayout llFavour=helper.getView(R.id.ll_favour);
        if ("0".equals(item.commentCount)){
            llComment.setVisibility(View.GONE);
        }else {
            llComment.setVisibility(View.VISIBLE);
        }
        if ("0".equals(item.favourCount)){
            llFavour.setVisibility(View.GONE);
        }else {
            llFavour.setVisibility(View.VISIBLE);
        }
        helper.setText(R.id.tv_comment, String.format("%s", item.commentCount + ""));
        helper.setText(R.id.tv_favour, String.format("%s", item.favourCount + ""));
    }
}
